package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.mapper.TbGoodsDescMapper;
import com.dongdongshop.mapper.TbGoodsMapper;
import com.dongdongshop.mapper.TbItemMapper;
import com.dongdongshop.pojo.GoodsVO;
import com.dongdongshop.pojo.TbGoods;
import com.dongdongshop.pojo.TbGoodsDesc;
import com.dongdongshop.pojo.TbItem;
import com.dongdongshop.service.GoodService;
import com.dongdongshop.util.PageBean;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @Description
 * @ClassName GoodServiceImpl
 * @Author RC
 * @date 2020.12.13 15:14
 */
@Component
@Service
public class GoodServiceImpl implements GoodService {


    @Autowired
    private TbGoodsMapper tbGoodsMapper;
    @Autowired
    private TbGoodsDescMapper tbGoodsDescMapper;
    @Autowired
    private TbItemMapper tbItemMapper;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public void saveGoods(TbGoods tbGoods, TbGoodsDesc tbGoodsDesc, List<TbItem> tbItems) {
        tbGoodsMapper.saveGoodsReturnId(tbGoods);
        tbGoodsDesc.setGoodsId(tbGoods.getId());
        tbGoodsDescMapper.insertSelective(tbGoodsDesc);
        for (TbItem item: tbItems ){
            item.setCreateTime(new Date());
            item.setUpdateTime(new Date());
            item.setGoodsId(tbGoods.getId());
            item.setSellerId(tbGoods.getSellerId());
            item.setStatus("1");
            item.setTitle(tbGoods.getGoodsName());
            tbItemMapper.insertSelective(item);
        }
    }

    @Override
    public PageBean<GoodsVO> getGoodsList(Integer pageNumber, Integer pageSize, TbGoods tbGoods) {
        PageBean page = new PageBean();
        page.setPageNumber(pageNumber);
        page.setPageSize(pageSize);
        List<GoodsVO> list = tbGoodsMapper.getGoodSList(page.getStartIndex(),pageSize,tbGoods);
        //分页查询
        int i = tbGoodsMapper.selectCounts();
        page.setTotalCount(i);
        page.setResult(list);
        return page;

    }
    //运营商审核商家商品
    @Override
    public void getGoodsStatusById(Long[] ids) {
      int i = tbGoodsMapper.UpdateGoodsStatusById(ids);
      if(i > 0){
          //审核通过发送mq消息
          rocketMQTemplate.convertAndSend("approvedSendSuccess",ids);
      }
    }
    //运营商驳回审核请求
    @Override
    public void rejectGoodsStatusById(Long[] ids) {
       int i = tbGoodsMapper.rejectGoodsStatusById(ids);
    }
    //商家提交审核
    @Override
    public void updateStatusByIds(Long[] ids) {
        tbGoodsMapper.updateStatusByIds(ids);
    }

    @Override
    public void deleteBath(Long[] ids) {
        tbGoodsMapper.deleteBath(ids);
    }
    //运营商展示全部商品
    @Override
    public PageBean<GoodsVO> getGoodsListAll(Integer pageNumber, Integer pageSize, TbGoods tbGoods) {
        PageBean page = new PageBean();
        page.setPageNumber(pageNumber);
        page.setPageSize(pageSize);
        List<GoodsVO> list = tbGoodsMapper.getGoodsListAll(page.getStartIndex(),pageSize,tbGoods);
        //分页查询
        int i = tbGoodsMapper.selectCounts();
        page.setTotalCount(i);
        page.setResult(list);
        return page;

    }

    @Override
    public TbGoods findGoodsById(Long goodsId) {
        return tbGoodsMapper.selectByPrimaryKey(goodsId);
    }
}